/**
 * @description 获取当前时间
 * @return string
 */
export function getTimeState() {
	// 获取当前时间
	let timeNow = new Date();
	// 获取当前小时
	let hours = timeNow.getHours();
	// 判断当前时间段
	if (hours >= 6 && hours <= 10) return `早上好 ⛅`;
	if (hours >= 10 && hours <= 14) return `中午好 🌞`;
	if (hours >= 14 && hours <= 18) return `下午好 🌞`;
	if (hours >= 18 && hours <= 24) return `晚上好 🌛`;
	if (hours >= 0 && hours <= 6) return `凌晨好 🌛`;
}

/**
 * @description 判断数据类型
 * @param {Any} val 需要判断类型的数据
 * @return string
 */
export function isType(val: any) {
	if (val === null) return "null";
	if (typeof val !== "object") return typeof val;
	else return Object.prototype.toString.call(val).slice(8, -1).toLocaleLowerCase();
}

/**
 * @description 使用递归，过滤出需要渲染在左侧菜单的列表（剔除 isHide == true 的菜单）
 * @param {Array} menuList 所有菜单列表
 * @return array
 * */
export function getShowMenuList(menuList: Menu.MenuOptions[]) {
	let newMenuList: Menu.MenuOptions[] = JSON.parse(JSON.stringify(menuList));
	return newMenuList.filter(item => {
		item.children?.length && (item.children = getShowMenuList(item.children));
		return !item.meta?.isHide;
	});
}

/**
 * @description 使用递归，过滤出当前路径匹配的面包屑地址
 * @param {String} path 当前访问地址
 * @param {Array} menuList 所有菜单列表
 * @returns array
 */
export function getCurrentBreadcrumb(path: string, menuList: Menu.MenuOptions[]) {
	let tempPath: Menu.MenuOptions[] = [];
	try {
		const getNodePath = (node: Menu.MenuOptions) => {
			tempPath.push(node);
			if (node.path === path) throw new Error("Find IT!");
			if (node.children?.length) node.children.forEach(item => getNodePath(item));
			tempPath.pop();
		};
		menuList.forEach(item => getNodePath(item));
	} catch (e) {
		return tempPath;
	}
}

/**
 * @description 双重递归找出所有面包屑存储到 pinia/vuex 中
 * @param {Array} menuList 所有菜单列表
 * @returns array
 */
export function getAllBreadcrumbList(menuList: Menu.MenuOptions[]) {
	let handleBreadcrumbList: { [key: string]: any } = {};
	// 这是函数
	const loop = (menuItem: Menu.MenuOptions) => {
		if (menuItem?.children?.length) menuItem.children.forEach(item => loop(item));
		handleBreadcrumbList[menuItem.path] = getCurrentBreadcrumb(menuItem.path, menuList);
	};
	menuList.forEach(item => loop(item));
	return handleBreadcrumbList;
}

/**
 * @description 使用递归，过滤需要缓存的路由
 * @param {Array} menuList 所有菜单列表
 * @param {Array} cacheArr 缓存的路由菜单 name ['**','**']
 * @return array
 * */
export function getKeepAliveRouterName(menuList: Menu.MenuOptions[], keepAliveArr: string[] = []) {
	menuList.forEach(item => {
		item.meta.isKeepAlive && item.name && keepAliveArr.push(item.name);
		item.children?.length && getKeepAliveRouterName(item.children, keepAliveArr);
	});
	return keepAliveArr;
}

/**
 * @description 扁平化数组对象(主要用来处理路由菜单),把菜单全部处理成一维数组
 * @param {Array} menuList 所有菜单列表
 * @return array
 */
export function getFlatArr(menuList: Menu.MenuOptions[]) {
	let newMenuList: Menu.MenuOptions[] = JSON.parse(JSON.stringify(menuList));
	return newMenuList.reduce((pre: Menu.MenuOptions[], current: Menu.MenuOptions) => {
		let flatArr = [...pre, current];
		if (current.children) flatArr = [...flatArr, ...getFlatArr(current.children)];
		return flatArr;
	}, []);
}

/**
 * @description: 构造树形结构
 * @param {any} data
 * @param {string} id
 * @param {string} parentId
 * @param {string} children
 * @return {*}
 */
// export function handleTree(data: any[], id?: string, value?: string, parentId?: string, children?: string) {
// 	const config = {
// 		id: id || "id",
// 		value: value || "value",
// 		parentId: parentId || "parentId",
// 		childrenList: children || "children"
// 	};

// 	const childrenListMap = {} as any;
// 	const nodeIds = {} as any;
// 	const tree = [];

// 	for (const d of data) {
// 		const parentId = d[config.parentId];
// 		if (childrenListMap[parentId] == null) {
// 			childrenListMap[parentId] = [];
// 		}
// 		nodeIds[d[config.id]] = d;
// 		nodeIds[d[config.value]] = d;
// 		childrenListMap[parentId].push(d);
// 	}

// 	for (const d of data) {
// 		const parentId = d[config.parentId];
// 		if (nodeIds[parentId] == null) {
// 			tree.push(d);
// 		}
// 	}

// 	for (let t of tree) {
// 		adaptToChildrenList(t);
// 	}

// 	function adaptToChildrenList(o: { [x: string]: any }) {
// 		if (childrenListMap[o[config.id]] !== null) {
// 			o[config.childrenList] = childrenListMap[o[config.id]];
// 		}
// 		if (o[config.childrenList]) {
// 			for (let c of o[config.childrenList]) {
// 				adaptToChildrenList(c);
// 			}
// 		}
// 	}
// 	return tree;
// }
